package com.mbti.youmore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbti.youmore.common.exception.UserNotExistsException;
import com.mbti.youmore.entity.Follow;
import com.mbti.youmore.entity.User;
import com.mbti.youmore.mapper.UserMapper;
import com.mbti.youmore.service.FollowService;
import com.mbti.youmore.mapper.FollowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

/**
* @author Guqier
* @description 针对表【t_follow】的数据库操作Service实现
* @createDate 2024-05-08 20:30:45
*/
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow>
    implements FollowService{

    @Autowired
    private FollowMapper followMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Map<String, Object>> getFansInfo(Integer userId) {
        List<Map<String, Object>> res = new ArrayList<>();
        List<User> fans = followMapper.selectByFollowedUserId(userId);

        for (User fan : fans) {
            Map<String, Object> fanMap = new LinkedHashMap<>();

            fanMap.put("userId", fan.getUserId());
            fanMap.put("nickname", fan.getNickname());
            fanMap.put("profilePic", fan.getProfilePic());
            fanMap.put("userLang", fan.getUserLang());
            fanMap.put("userLocation", fan.getUserLocation());

            List<Follow> follows = followMapper.selectList(new LambdaQueryWrapper<Follow>().eq(Follow::getFollowerUserId, userId).eq(Follow::getFollowedUserId, fan.getUserId()));
            if (follows.isEmpty()) {
                fanMap.put("isFollowing", false);
            } else {
                fanMap.put("isFollowing", true);
            }

            res.add(fanMap);
        }

        return res;
    }

    @Override
    public List<Map<String, Object>> getFollowedUser(Integer userId) {
        List<Map<String, Object>> res = new ArrayList<>();
        List<User> users = followMapper.selectByFollowerUserId(userId);

        for (User user : users) {
            Map<String, Object> fanMap = new LinkedHashMap<>();

            fanMap.put("userId", user.getUserId());
            fanMap.put("nickname", user.getNickname());
            fanMap.put("profilePic", user.getProfilePic());
            fanMap.put("userLang", user.getUserLang());
            fanMap.put("userLocation", user.getUserLocation());

            res.add(fanMap);
        }

        return res;
    }

    @Override
    @Transactional
    public boolean follow(Integer currentUserId, Integer targetUserId) {
        User targetUser = userMapper.selectById(targetUserId);
        if (targetUser == null) {
            throw new UserNotExistsException("用户不存在");
        }

        Follow follow = new Follow();
        follow.setFollowerUserId(currentUserId);
        follow.setFollowedUserId(targetUserId);
        follow.setFollowTime(new Timestamp(new Date().getTime()));
        int res = followMapper.insert(follow);

        return res == 1;
    }

    @Override
    @Transactional
    public boolean cancelFollow(Integer currentUserId, Integer targetUserId) {
        User targetUser = userMapper.selectById(targetUserId);
        if (targetUser == null) {
            throw new UserNotExistsException("用户不存在");
        }

        int res = followMapper.delete(new LambdaQueryWrapper<Follow>().eq(Follow::getFollowerUserId, currentUserId).eq(Follow::getFollowedUserId, targetUserId));

        return res == 1;
    }

    @Override
    public boolean isFollowEach(Integer currentUserId, Integer targetUserId) {
        List<Follow> follows1 = followMapper.selectList(new LambdaQueryWrapper<Follow>().eq(Follow::getFollowerUserId, currentUserId).eq(Follow::getFollowedUserId, targetUserId));
        List<Follow> follows2 = followMapper.selectList(new LambdaQueryWrapper<Follow>().eq(Follow::getFollowerUserId, targetUserId).eq(Follow::getFollowedUserId, currentUserId));

        return !follows1.isEmpty() && !follows2.isEmpty();
    }

}




